

// Declare a unique namespace.
var attunet = {};

// Class constructor. Parameter container is a DOM elementon the client that
// that will contain the visualization.
attunet.USAStateMap = function(container) {
  this.containerElement = container;
}

attunet.StateShape =function(surface, coord){
  this.shape=surface.createPolyline(coord);
  //this.shape.connect("onmouseover", this, this.onMouseOver);
  dojo.connect(this.shape.getEventSource(), "onmouseover", this, attunet.StateShape.onMouseOver );  
  dojo.connect(this.shape.getEventSource(), "onmouseout", this, attunet.StateShape.onMouseOut );  
}

attunet.StateShape.onMouseOut =function(evt){
  this.shape.setFill(this.color);
}
attunet.StateShape.onMouseOver =function(evt){
  this.shape.setFill("yellow");
}

attunet.StateShape.prototype.setFill =function(col){
  this.shape.setFill(col);
}

/*attunet.USAStateMap.prototype.getMapCoord = function(){
  var mapCoord;
  var xhrArgs = {
      //url: "http://attunet.googlecode.com/svn/trunk/usmap/USMapCoord.xml",
      url: "USMapCoord.xml",
      handleAs: "xml",
preventCache: true,
      sync: true,
      load: function(data) {
       mapCoord = data;
       return mapCoord;
  },
  error: function(error) {
    print("Error");
        }
  }

 //Call the asynchronous xhrGet
 var deferred = dojo.xhrGet(xhrArgs);
 return mapCoord;
} */

attunet.USAStateMap.prototype.getMapCoord = function() {
  var xmlstring = '<?xml version="1.0"?>\
<map id="USStateMap"> \
<area shape="poly" alt="california" coords="44,153,75,161,68,191,100,239,102,242,102,248,101,253,99,259,96,269,76,263,71,249,65,245,60,239,53,238,51,231,50,222,47,215,47,209,46,204,48,198,43,192,41,179,39,168" href="#" title="California" />\
<area shape="poly" alt="newmexico" coords="148,228,196,232,193,285,149,284,148,289,140,288" title="New Mexico" />\
<area shape="poly" alt="nevada" coords="75,161,118,169,110,222,108,230,103,233,102,241,68,191"  title="Nevada" />\
<area shape="poly" alt="arizona" coords="110,221,147,228,140,288,125,287,96,270,98,262,102,251,102,241,103,233,108,230" href="#" title="Arizona" />\
<area shape="poly" alt="utah" coords="118,169,140,172,139,184,153,186,148,228,110,222" href="#" title="Utah" />\
<area shape="poly" alt="colorado" coords="154,186,204,190,203,232,149,229" href="#" title="Colorado" />\
<area shape="poly" alt="texas" coords="195,239,219,239,219,259,235,268,260,269,264,271,267,272,269,288,271,297,271,308,268,315,263,316,263,312,259,313,259,319,243,328,240,333,239,349,241,353,237,352,227,351,223,343,218,333,213,325,209,317,205,313,198,310,189,319,177,312,175,300,163,285,192,285" href="#" title="Texas" />\
<area shape="poly" alt="oregon" coords="59,111,63,111,67,118,104,123,104,123,107,128,102,139,100,148,96,165,74,161,44,153,45,144,49,137" href="#" title="Oregon" />\
<area shape="poly" alt="washington" coords="60,87,69,95,68,104,71,102,74,95,72,89,73,86,110,93,104,123,67,118,63,111,58,111" href="#" title="Washington" />\
<area shape="poly" alt="idaho" coords="110,94,119,96,117,110,117,116,123,120,123,125,120,129,127,133,128,141,133,145,144,145,140,173,117,169,96,165,102,139,104,134,107,128,104,124" href="#" title="Idaho" />\
<area shape="poly" alt="montana" coords="119,96,196,107,192,147,144,143,144,146,133,146,128,141,127,134,120,129,123,125,123,120,117,116" href="#" title="Montana" />\
<area shape="poly" alt="northdakota" coords="196,106,240,108,244,125,244,134,244,138,193,137" href="#" title="North Dakota" />\
<area shape="poly" alt="southdakota" coords="193,137,244,138,244,143,247,145,247,164,245,174,226,171,192,168" href="#" title="South Dakota" />\
<area shape="poly" alt="wyoming" coords="144,143,193,147,191,189,153,186,139,184" href="#" title="Wyoming" />\
<area shape="poly" alt="oklahoma" coords="196,232,262,234,263,240,263,269,235,267,219,260,218,239,195,240" href="#" title="Oklahoma" />\
<area shape="poly" alt="kansas" coords="204,201,254,201,257,203,261,211,262,234,203,232" href="#" title="Kansas" />\
<area shape="poly" alt="nebraska" coords="192,168,245,173,248,179,254,201,204,201,204,190,191,189" href="#" title="Nebraska" />\
<area shape="poly" alt="alaska" coords="13,19,25,11,29,8,38,12,52,15,56,19,62,62,65,62,70,65,74,62,77,65,92,76,94,83,90,84,88,80,76,70,70,70,56,64,47,62,43,67,38,67,41,60,38,59,34,64,30,68,31,72,18,83,12,85,7,86,9,81,22,73,22,69,18,70,13,69,12,61,9,64,5,56,5,51,11,46,16,45,17,40,9,39,6,33,11,30,16,31,20,30" href="#" title="Alaska" />\
<area shape="poly" alt="louisiana" coords="268,278,291,276,292,285,291,288,289,296,303,297,303,302,299,304,299,306,307,307,307,312,311,316,305,316,299,317,293,316,288,312,280,314,273,311,269,304,272,301,268,287" href="#" title="Louisiana" />\
<area shape="poly" alt="arkansas" coords="263,239,297,239,300,242,293,261,291,269,292,276,268,278,267,272,263,269" href="#" title="Arkansas" />\
<area shape="poly" alt="missouri" coords="253,196,282,196,283,204,289,208,289,211,294,212,293,218,298,223,299,227,300,231,303,231,304,235,301,239,300,242,296,239,263,239,262,213" href="#" title="Missouri" />\
<area shape="poly" alt="iowa" coords="247,165,283,163,284,170,289,175,290,178,290,183,286,186,285,193,282,196,253,196,249,185,248,180,245,173" href="#" title="Iowa" />\
<area shape="poly" alt="minnesota" coords="240,108,252,108,254,104,263,112,274,113,276,116,281,116,284,114,289,116,281,123,277,126,274,130,273,138,270,141,271,147,271,151,282,158,283,163,247,165,247,146,244,144,243,125,242,115" href="#" title="Minnesota" />\
<area shape="poly" alt="maine" coords="420,122,422,107,425,95,432,95,436,99,438,108,446,118,446,122,440,126,435,130,430,134,429,141,423,134" href="#" title="Main" />\
<area shape="poly" alt="newhampshire" coords="420,122,423,133,428,140,427,144,424,148,417,150,415,139,416,128" href="#" title="New Hampshire" />\
<area shape="poly" alt="vermont" coords="404,128,415,125,417,149,411,151,408,142" href="#" title="Vermont" />\
<area shape="poly" alt="florida" coords="323,301,323,294,343,292,369,293,370,290,375,289,392,322,394,327,396,334,394,340,390,346,380,340,378,333,375,330,369,321,363,310,356,301,349,303,343,305,340,300,332,298" href="#" title="Florida" />\
<area shape="poly" alt="mississippi" coords="297,253,314,253,315,302,308,304,303,302,303,297,289,296,292,285,291,274,292,262" href="#" title="Mississippi" />\
<area shape="poly" alt="alabama" coords="314,253,335,250,341,274,343,292,332,293,323,294,323,301,316,301,315,277" href="#" title="Alabama" />\
<area shape="poly" alt="georgia" coords="335,250,352,248,369,264,377,273,375,280,374,288,369,291,369,292,343,292,340,269" href="#" title="Georgia" />\
<area shape="poly" alt="southcarolina" coords="354,248" href="#" title="South Carolina" />\
<area shape="poly" alt="wisconsin" coords="274,132,282,128,286,130,293,127,298,120,305,119,300,124,306,127,310,129,317,125,321,125,324,127,330,131,325,132,317,133,311,136,307,140,307,143,310,142,308,153,308,162,308,171,288,175,284,168,283,162,278,156,272,152,270,144" href="#" title="Wisconsin" />\
<area shape="poly" alt="illinois" coords="289,175,308,171,311,177,311,182,314,209,313,213,312,222,310,228,304,231,299,229,297,221,294,219,293,212,289,211,289,208,284,204,283,196,287,186,291,182" href="#" title="Illinois" />\
<area shape="poly" alt="tennessee" coords="297,253,302,238,313,237,314,233,345,230,363,229,358,235,351,241,346,245,344,249,335,250,314,253" href="#" title="Tennessee" />\
<area shape="poly" alt="southcarolina" coords="352,248,357,244,371,242,373,245,383,244,391,251,389,260,384,266,379,270,377,274,367,262" href="#" title="SouthCarolina" />\
<area shape="poly" alt="northcarolina" coords="363,229,401,219,406,216,409,222,403,226,407,226,410,229,405,237,401,243,398,249,392,251,383,244,374,245,371,242,366,243,360,243,352,248,345,248,348,244" href="#" title="North Carolina" />\
<area shape="poly" alt="indiana" coords="311,181,318,176,332,176,336,205,334,209,328,217,321,219,311,222,314,206" href="#" title="Indiana" />\
<area shape="poly" alt="michigan" coords="318,144,321,143,324,140,325,133,331,136,336,139,337,144,337,149,335,153,335,155,339,152,343,152,344,156,345,161,342,173,337,176,332,176,318,176,319,163,316,156" href="#" title="Michigan" />\
<area shape="poly" alt="kentucky" coords="336,206,345,208,351,209,353,214,358,219,352,226,346,230,314,233,313,237,303,239,306,230,310,228,312,220,317,221,324,218,330,215" href="#" title="kentucky" />\
<area shape="poly" alt="ohio" coords="332,176,344,177,349,178,358,171,361,169,364,186,363,193,358,201,352,209,349,209,344,207,336,206,335,200,334,192" href="#" title="ohio" />\
<area shape="poly" alt="pennsylvania" coords="361,169,365,166,366,169,397,162,403,167,404,178,402,184,398,186,366,194,364,188,362,175" href="#" title="pennsylvania" />\
<area shape="poly" alt="newyork" coords="371,153,381,152,386,149,387,143,390,135,398,130,404,129,411,150,412,167,411,170,403,167,398,162,366,169,365,166,371,160" href="#" title="newyork" />\
<area shape="poly" alt="virginia" coords="357,221,365,221,371,215,373,207,378,205,382,194,387,194,391,196,394,203,400,208,400,218,364,229,346,230" href="#" title="virginia" />\
<area shape="poly" alt="westvirginia" coords="354,207,363,193,363,183,366,193,374,192,374,197,379,193,382,194,378,205,373,207,371,215,365,221,359,221,353,215,351,210" href="#" title="westvirginia" />\
<area shape="poly" alt="massachusetts" coords="412,151,423,149,426,144,429,145,428,150,426,154,411,157" href="#" title="massachusetts" />\
<area shape="poly" alt="rhodeisland" coords="423,155,427,153,432,153,435,152,435,157,431,159,426,161,422,159" href="#" title="rhodeisland" />\
<area shape="poly" alt="connecticut" coords="412,157,422,155,424,160,427,160,420,164,415,168,412,170" href="#" title="connecticut" />\
<area shape="poly" alt="newjersey" coords="404,168,410,170,420,165,423,166,412,173,411,185,408,191,401,185,404,181,405,176" href="#" title="newjersey" />\
<area shape="poly" alt="delaware" coords="400,187,404,187,406,195,407,206,406,211,402,203,399,198,397,192" href="#" title="delaware" />\
<area shape="poly" alt="" coords="386,189" href="#" title="" />\
<area shape="poly" alt="maryland" coords="374,191,382,190,393,187,401,185,400,187,398,191,398,196,402,203,400,206,394,203,393,199,388,194,381,193,375,196" href="#" title="maryland" />\
<area shape="poly" alt="hawaii" coords="64,283,69,285,73,289,75,294,67,299,63,301,63,297,62,292,62,288" href="#" title="hawaii" />\
</map>';

  var xmlobject;

if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlobject=parser.parseFromString(xmlstring,"text/xml");
  }
else // Internet Explorer
  {
  xmlobject=new ActiveXObject("Microsoft.XMLDOM");
  xmlobject.async="false";
  xmlobject.loadXML(xmlstring); 
  } 

  
return xmlobject;
}

attunet.USAStateMap.prototype.scrubData = function(data){
          
         var stateValuesHash = new Array();
         var rows = data.getNumberOfRows();
         var stateName ="";
         for(i=0; i< rows; i++) {
           stateName = data.getValue(i,0); 
           stateName = stateName.replace(/\s+/g, ""); // Remove spaces. 
           stateName = stateName.toLowerCase();
           stateValuesHash[stateName] = data.getValue(i,1);
         }
  return stateValuesHash;
}

// Main drawing logic.
// Parameters: 2
//   data is data to display, type google.visualization.DataTable.
//   options is a name/value map of options. Our attunet takes one option.
attunet.USAStateMap.prototype.draw = function(data, options) {

        dojo.require("dojox.gfx");
        dojo.require("dojox.gfx.shape");
        
        
        this.data = data;
        this.options = options;        
        dojo.addOnLoad( this, attunet.USAStateMap.prototype.drawMap);

}
attunet.USAStateMap.prototype.drawMap = function() {
      var data = this.data;
       var options = this.options;
        console.log(data);
        var stateValuesHash = attunet.USAStateMap.prototype.scrubData(data);
         mapCoord = attunet.USAStateMap.prototype.getMapCoord();
         var areasArray = mapCoord.getElementsByTagName("area");
        this.containerElement.innerHTML = "";

         
         options.defaultColor = (typeof(options.defaultColor) == 'undefined')? '#C0C0C0': options.defaultColor;
         options.isDataColor = (typeof(options.isDataColor) == 'undefined')? true : options.isDataColor;
         options.isDataColor = (options.isDataColor == null)? true : options.isDataColor;
         options.width = (typeof(options.width) == 'undefined')? 450: options.width;
         options.height = (typeof(options.height) == 'undefined')? 400 : options.height;

         var surface = dojox.gfx.createSurface(this.containerElement, options.width, options.height);
         var count = 0;

         for(var i=0; i< areasArray.length ;i++) {
          var coords = areasArray[i].getAttribute("coords");
           var stateName = areasArray[i].getAttribute("alt");
           var coordStringArray = coords.split(",");
           var coordArray = new Array(coordStringArray.length);
           for(var j=0; j< coordStringArray.length;j++) {
             coordArray[j] = parseInt(coordStringArray[j]);
           }     

         var stateShape =  new attunet.StateShape(surface, coordArray);

         
         if(options.isDataColor ==true ){
            if(typeof(stateValuesHash[stateName])== 'undefined') 
              stateShape.color =  options.defaultColor 
             else
               stateShape.color = stateValuesHash[stateName];
         }
         else {

           if ((options.color == null) || (typeof(stateValuesHash[stateName]) == 'undefined'))
             stateShape.color = options.defaultColor;
           else {
               var val = stateValuesHash[stateName];
               stateShape.color = (options.color[val]  != null) ? options.color[val]: options.defaultColor;
            }
         }
         stateShape.shape.setStroke(null);
        stateShape.shape.setFill(stateShape.color);
         
        
       }
}

